#!/usr/bin/python
# coding=utf-8
import requests
import sys
import os
from queue import Queue
import threading
from agent_proxy import user_agent_list



class DirScanMain:
    """docstring for DirScanMain"""

    def __init__(self, options):
        self.url = options['url']
        self.type = options['type']
        self.filename = options['filename']
        self.count = options['thread_count']

    class DirScan(threading.Thread):
        """docstring for DirScan"""

        def __init__(self, queue, total):
            threading.Thread.__init__(self)
            self._queue = queue
            self._total = total

        def run(self):
            while not self._queue.empty():
                url = self._queue.get()

                #
                threading.Thread(target=self.msg).start()

                try:
                    r = requests.get(url=url, headers=user_agent_list.get_user_agent(), timeout=8, )
                    if r.status_code == 200 or r.status_code == 500:
                        sys.stdout.write('\r' + '[+]%s\t\t\n' % (url))
                        result = open('result.html', 'a+')
                        result.write('<a href="' + url + '" target="_blank">' + url + '</a>')
                        result.write('\r\n</br>')
                        result.close()
                except Exception as e:
                    pass

        def msg(self):
            # print self._total,self._queue.qsize()
            per = 100 - float(self._queue.qsize()) / float(self._total) * 100
            percentage = "%s Finished| %s All| Scan in %1.f %s" % (
            (self._total - self._queue.qsize()), self._total, per, '%')
            sys.stdout.write('\r' + '[*]' + percentage)

    def start(self):
        result = open('result.html', 'w')
        result.close()
        queue = Queue()
        # 这里就是要获取有关类型的文件了，然后集体弄进queue队列中去
        dirlist = os.listdir('./dictionary')

        for i in dirlist:
            if self.type in i.lower():
                f = open('./dictionary/%s' % i, 'r')
                for j in f:
                    prefix = j.find("/",0,1)
                    if prefix == 0:
                        j = j[1:len(j)]
                    queue.put(self.url + j.rstrip('\n'))

            if self.type == 'other' and self.type not in i.lower():
                f = open('./dictionary/%s' % i, 'r')
                for j in f:
                    prefix = j.find("/", 0, 1)
                    if prefix == 0:
                        j = j[1:len(j)]
                    queue.put(self.url + j.rstrip('\n'))

            if self.type == 'all' :
                f = open('./dictionary/%s' % i, 'r')
                for j in f:
                    prefix = j.find("/", 0, 1)
                    if prefix == 0:
                        j = j[1:len(j)]
                    queue.put(self.url + j.rstrip('\n'))
        #
        total = queue.qsize()

        threads = []
        thread_count = int(self.count)

        for i in range(thread_count):
            threads.append(self.DirScan(queue, total))
        for i in threads:
            i.start()

        print('------------------program is staring-----------------------\n')
        for i in threads:
            i.join()
        print('------------------program runing finish -----------------------\n')

